﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using WindowsFormsApp2.类;
using System.Windows;


namespace YJYpublicClass
{
    public class 外设
    {
        //PLC
        //光源控制器
        //   public static HZlightControl HZLightControl = new HZlightControl();
        public static PLC_Modulbus mudbus = new PLC_Modulbus();
        public static HKCamera Camera = new HKCamera();
        public static HKCamera Camera2 = new HKCamera();
        public static HKCamera Camera3 = new HKCamera();//直接将各硬件控制类的实例化作为静态成员调用，而不是在mainform里实例
        public static Socketconnect DuMaQi1 = new Socketconnect();


        //数据库连接字符串
        //   public static SqlConn SqlConnstr = new SqlConn();
        public static GongyiVars RGongyiVar = new GongyiVars(); 
        //光源控制器
        public static DWL_24W60_T8 LightCtrl_2D = new DWL_24W60_T8();
        public static HDPL_24W350_T2 HDPL_24W60_1 = new HDPL_24W350_T2();
        public static DGL_24W100_T4 LightCtrl_xiansao1 = new DGL_24W100_T4();
        public static DGL_24W100_T4 LightCtrl_xiansao2 = new DGL_24W100_T4();

        public static SD2_24W124 LightCtrl = new SD2_24W124();

        //两类参数
        public static S1stationPrasC S1stationPrasR = new S1stationPrasC();

        public static peizhiPras PZpras = new peizhiPras();
       
    }
    //全局变量
    public class PublicValue
    {
        //♦♦♦♦主界面♦♦♦♦
        public static string SystemIniPath;                 //各窗体的INI文件路径
        public static string CameraIniPath;
        public static string ComIniPath;
        public static string xmlpath;
        public static string PLCconfigpath = System.AppDomain.CurrentDomain.BaseDirectory + "PLCconfig.xml";
        public static string Lightconfigpath_2D = System.AppDomain.CurrentDomain.BaseDirectory + "2D_LightControl.xml";
        public static string Lightconfigpath_xiansao1 = System.AppDomain.CurrentDomain.BaseDirectory + "xiansao1_LightControl.xml";
        public static string Lightconfigpath_xiansao2 = System.AppDomain.CurrentDomain.BaseDirectory + "xiansao2_LightControl.xml";
        public static string StationPramaspath = System.AppDomain.CurrentDomain.BaseDirectory + "StationPramas.xml";

        public static string S1prassavepath = System.AppDomain.CurrentDomain.BaseDirectory + "S1pras.xml";
        public static string S2prassavepath = System.AppDomain.CurrentDomain.BaseDirectory + "S2pras.xml";
        public static string S3prassavepath = System.AppDomain.CurrentDomain.BaseDirectory + "S3pras.xml";
        public static string PZprassavepath = System.AppDomain.CurrentDomain.BaseDirectory + "PZpras.xml";
        public static string S1Detectofpath = System.AppDomain.CurrentDomain.BaseDirectory + "S1DPofpras.xml";
        public static string S1modelImagepath = System.AppDomain.CurrentDomain.BaseDirectory + "S1modelImage.bmp";

        //滚轮架零点参数

        

        
        //千斤顶2工作位置
        public static double[] QJDPos =new double[] {200,350};

        
        
        //激光位置
        public static double LaserPos = 0;

        //得到的理论错边值


        public static double HeightDisCorrect = 0;
        //通讯参数
        public static string PLCresult = "";
        public static string S2PLCresult = "";
        public static string S3PLCresult = "";
        public static string lastPLCstatus = "0";
        public static string newPLCstatus = "0";
        public static string S2lastPLCstatus = "0";
        public static string S2newPLCstatus = "0";
        public static string S3lastPLCstatus = "0";
        public static string S3newPLCstatus = "0";

        public static string S1ScanPLCresult = "";
        public static string S2ScanPLCresult = "";
        public static string S1ScanlastPLCstatus = "0";
        public static string S1ScannewPLCstatus = "0";
        public static string S2ScanlastPLCstatus = "0";
        public static string S2ScannewPLCstatus = "0";
        public static string S3ScanPLCresult = "";
        public static string S3ScanlastPLCstatus = "0";
        public static string S3ScannewPLCstatus = "0";

        public static string SNNow = "";



        //计算的错边和间隙
        public static double CurrentHeightDis;
        public static double CurrentGap;

        //轮廓默认保存位置
        public static string CSVRootPath = AppDomain.CurrentDomain.BaseDirectory + "CSV";



        #region[♦♦♦♦相机配置♦♦♦♦]
        //public static double exposuretime = 500.0;   //相机曝光时间 //华为揽
        //public static double exposuretime2 = 500.0;   //相机曝光时间 //华为揽
        //public static double exposuretime3 = 500.0;   //相机曝光时间 //华为揽
        public static UInt32 ImageHeigt = 4992;
        public static UInt32 Rowfreq = 5000;

        public static string[] CameraSerialNum = new string[6];  //定义3个相机的序列号
        public static double[] DexposureTime = new double[6];   //相机曝光时间
        public static double[] InPramas = new double[6];// 定义可调参数
        public static double[] InPramas2 = new double[6];

        public static bool[] isLiveFlag = new bool[]{false,false,false};

        #endregion

        #region【♦♦♦♦系统配置♦♦♦♦】
        public static string LogPath;                       //系统日志存放路径
        public static string HelpFilePath;                  //软件帮助文档存放路径
        public static string SaveImagePath;
        public static string ResultCsvpath;                 //结果保存路径

        public static int DeleteImageTime;                  //文件删除时间间隔
        public static int DeleteLogTime;                    //日志删除时间间隔

        public static string LightTime;                     //产量自动清除变量<白班清除时间>
        public static string NightTime;                     //晚班清除时间
        public static bool ProductRecord;                   //是否保存产量文档标识
        public static int ProductRecordDel;                 //产量保存文档删除间隔
        public static string ProductRecordPath;             //产量保存路径
        public static string AppDirectory = null;

        public static string[] StationINIpath= new string[6];

        #endregion【♦♦♦♦系统配置♦♦♦♦】

        #region【♦♦♦♦权限配置♦♦♦♦】
        public static Int16 LoginRight=1;                  //用户等级1,普通用户，2技术员，3管理员

        #endregion【♦♦♦♦权限配置♦♦♦♦】

        #region【♦♦♦♦Hdvp函数变量♦♦♦♦】
        //public static string ProgramPath = @"\halcon函数\Imageprocess.hdev";    //设置运行的调用的Hdev文件   
        public static string[] mainHProgramPath = new string[6];//{null,null,null,"","" };


        public static HObject hv_image1 = null;
        public static HObject hv_image2 = null;
        public static HObject hv_image3 = null;
        public static HObject hv_image4 = null;
        public static HObject hv_image5 = null;
        public static HObject hv_image6 = null;

        public static string  EndResult = "";





        public static int hv_modetype = 1;       //参数类型

        #endregion【♦♦♦♦Hdvp函数变量♦♦♦♦】

        #region【♦♦♦♦产量统计变量♦♦♦♦】
        public static int OKnum;
        public static int NGnum;
        public static int totalnum;
        public static string Lianglv_s;

        #endregion【♦♦♦♦产量统计变量♦♦♦♦】

        #region【♦♦♦♦程序测试♦♦♦♦】
        public static bool test_chufa;
        #endregion【♦♦♦♦程序测试♦♦♦♦】

        #region【♦♦♦♦程序功能控制♦♦♦♦】
        public static int APPstatus = 0;                    //标识软件：初始状态0；运行状态1；停止（可以手动）-1 2；停止-2 3；急停按下-4；故障-5；复位过程中-6；
        public static bool saveOKImageflag=false;    //OK图片是否保存
        public static bool islivegrab = false;      //是否实时采图
        public static UInt16 LstartGrabFlag = 0;   //左取图标志位
        public static UInt16 RstartGrabFlag = 0;   //右取图标志位
        public static Boolean IsAbid3DCamera = false;  //
        public static Boolean AppResetedFlag = false;
        public static Boolean kongpaoFlag = false;
        
        // public static 

        public enum APPstatusE
        {
            初始状态=0,
            运行=1,
            手动=2,
            机台未复位 = 3,
            急停按下=4,
            故障=5,
            复位过程中=6
        }

        #endregion【♦♦♦♦程序功能控制♦♦♦♦】


        #region [校机参数]

        #endregion

        #region 【♦♦♦♦运动控制♦♦♦♦】
        //
        public static double detectUpline = 1.05;
        public static double detectDownline = 0.90;

        #endregion

        #region 【♦♦♦♦PLC通信配置♦♦♦♦】
        //
        public static string PLCIP = "192.168.1.1";   //IP地址
        public static string PLCport = "502";  //端口号

        #endregion
        #region 【♦♦♦♦光源通信配置♦♦♦♦】
        public static string LightSourceIP = "192.168.88.143";   //IP地址
        public static string LightSourcePort = "1000";   //IP地址
        #endregion

        #region 数据库连接
        public static string Connstr ="";
        #endregion
    }

    public class SqlConn
    {
        public  string ServerIp="127.0.0.1";
        public string ServerName = "疫苗瓶测试数据";
        public string ServerAccounts = "sa";
        public  string ServerPwd="123456";
    }
     //与PLC交互指令
    //PLC那边发挥的指令，
    //PLC发给这边指令，拍照，机器状态，控制字
        //我回PLC指令，收到拍照信号，处理中，处理完成，结果OK，结果NG，无拍照目标，
        //系统待机，系统自动、系统手动，系统异常
    public class S1_PLC_InF
    {
        //PLC发过来的信号
        public static IOstatus Stargrab = new IOstatus();    //启动拍照
        public static Boolean PLCRestart = false;  //系统重启
        public static double RoSpeed = 0.0;
        //发给PLC信息
        public static Boolean ReseiveGrab = false;
        public static Boolean _处理中 = false;
        public static Boolean _处理完成 = false;
        public static Boolean _结果OK = false;
        public static Boolean _结果NG = false;
        public static Boolean _无拍照目标 = false;
        public static Boolean _系统待机 = false;
        public static Boolean _系统自动 = false;
        public static Boolean _系统手动 = false;
        public static Boolean _系统异常 = false;
        //后续尺寸信息暂时不给
    }

    public class S2_PLC_InF
    {
        //PLC发过来的信号
        public static IOstatus Stargrab=new IOstatus();    //启动拍照
      //  public static Boolean PLCRestart = false;  //系统重启
        public static double RoSpeed = 0.0;
        //发给PLC信息
        public static Boolean ReseiveGrab = false;
        public static Boolean _处理中 = false;
        public static Boolean _处理完成 = false;
        public static Boolean _结果OK = false;
        public static Boolean _结果NG = false;
        public static Boolean _无拍照目标 = false;
        //后续尺寸信息暂时不给
    }

    public class S3_PLC_InF
    {
        //PLC发过来的信号
        public static IOstatus Stargrab = new IOstatus();    //启动拍照
        //  public static Boolean PLCRestart = false;  //系统重启
        public static double RoSpeed = 0.0;
        //发给PLC信息
        public static Boolean ReseiveGrab = false;
        public static Boolean _处理中 = false;
        public static Boolean _处理完成 = false;
        public static Boolean _结果OK = false;
        public static Boolean _结果NG = false;
        public static Boolean _无拍照目标 = false;
        //后续尺寸信息暂时不给
    }
    //再定义一个状态字
    public class ControlStaByte
    {
        public ControlStaByte()
        {
            //对变量进行初始化全部是false
            Array.Clear(Conbyte, 0, 16);
        }
        public void Init()
        {
            Array.Clear(Conbyte, 0, 16);
        }
       
        public Boolean[] Conbyte = new Boolean[16];
        //
        public UInt16 getWord()
        {
            UInt16 Value = 0;
            try
            {
                UInt16 temp = 0;
                for (UInt16 i = 0; i < 16; i++)
                {
                    if (Conbyte[i])
                    {
                        temp = (UInt16)(1 << i);
                    }
                    Value = (UInt16)(Value | temp);
                }
                return Value;
            }
            catch (Exception ex)
            {
                return Value;
            }

        }
    }

    //信号哦识别类
    public class IOstatus
    {
        public Boolean Cur = false;//只读不写
        Boolean Last = false;
        public Boolean UpEdge = false;  //上升沿
        public Boolean DownEdge = false; //下降沿

        //初始化
        public IOstatus()
        {
            Cur = false;
            Last = false;
            UpEdge = false;
            DownEdge = false;
        }
        //只设置状态，不清楚边缘状态，由外部清除
        public void setValue(Boolean value)
        {
            Cur = value;
            if(Cur!=Last)
            {
                if(Cur)
                {
                    UpEdge = true;
                }else
                {
                    DownEdge = true;
                }
                //不进行清空，只                        
                Last = Cur;
            }
        }
    }

    //工艺参数
    public class GongyiVars
    {
        public double S1intervel = 100;  //毫秒单位
        public double S1RoT = 100; //毫秒单位
        public double S1Tinterval = 200;
        //
        public Boolean S1Masked = false;
        public Boolean S2Masked = false;
        public Boolean S3Masked = false;
    }
    public class peizhiPras
    {
        //工位输出参数
        public Boolean S1outflag = false;
        public Boolean S2outflag = false;
        public Boolean S3outflag = false;
        public Boolean _启用统计 = false;
        public int SUM_OKNUM = 0;
        public int SUM_NGNUM = 0;
        public int SUM_Failed = 0;

        public int[] StaionOKNums;
        public int[] StaionNGNums;
        
        public int s1_OKNUM = 0;
        public int s1_NGNUM = 0;
        public int s2_OKNUM = 0;
        public int s2_NGNUM = 0;
        public int s3_OKNUM = 0;
        public int s3_NGNUM = 0;
        public int s4_OKNUM = 0;
        public int s4_NGNUM = 0;
        public int s5_OKNUM = 0;
        public int s5_NGNUM = 0;
        public int s6_OKNUM = 0;
        public int s6_NGNUM = 0;

        public int SUM = 0;
        public int ALLSUM = 0;
    }
}
